package com.springboot4layui.boot01.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot4layui.boot01.entity.Product;
import com.springboot4layui.boot01.entity.ProductCategory;
import com.springboot4layui.boot01.mapper.ProductCategoryMapper;
import com.springboot4layui.boot01.mapper.ProductMapper;
import com.springboot4layui.boot01.service.ProductService;
import com.springboot4layui.boot01.vo.DataVO;
import com.springboot4layui.boot01.vo.ProductVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired(required = false)
    private ProductMapper productMapper;
    @Autowired(required = false)
    private ProductCategoryMapper productCategoryMapper;
    @Override
    public DataVO<ProductVo> findData(Integer page,Integer limit) {
        DataVO dataVO = new DataVO();
        dataVO.setCode(0);
        dataVO.setMsg("");
//        dataVO.setCount(productMapper.selectCount(null));//分页Ipage 中有.getTotal()方法,不用单独赋值

        //分页查询
        IPage<Product> productIPage = new Page<>(page,limit);
        IPage<Product> result = productMapper.selectPage(productIPage, null);

        dataVO.setCount(result.getTotal());
//        List<Product> productList = productMapper.selectList(null);
        List<Product> productList = result.getRecords();
        List<ProductVo> productVoList = new ArrayList<>();
        for (Product product :productList) {
            ProductVo productVo = new ProductVo();
            productVo.setId(product.getId());
            BeanUtils.copyProperties(product,productVo);//会把pruduct中属性的数据,复制到productVo对应的属性中
            QueryWrapper wrapper = new QueryWrapper();

            wrapper.eq("id",product.getCategoryleveloneId());
            ProductCategory productCategory = productCategoryMapper.selectOne(wrapper);
            if(productCategory!=null){
                productVo.setCategorylevelone(productCategory.getName());
            }

            wrapper = new QueryWrapper();
            wrapper.eq("id",product.getCategoryleveltwoId());
            productCategory = productCategoryMapper.selectOne(wrapper);
            if(productCategory!=null){
                productVo.setCategoryleveltwo(productCategory.getName());
            }

            wrapper = new QueryWrapper();
            wrapper.eq("id",product.getCategorylevelthreeId());
            productCategory = productCategoryMapper.selectOne(wrapper);
            if(productCategory!=null){
                productVo.setCategorylevelthree(productCategory.getName());
            }
            productVoList.add(productVo);
        }
        dataVO.setData(productVoList);
        return dataVO;
    }
}
